<?php

class Framework {
	
	/**
	 * PRSP Launcher<br>
	 * Install adn start plugins
	 *
	 */
	public function launcher() {
		
		//Set timestamp
		$GLOBALS ['PRSP'] ['BeginTime'] = microtime ();
		
		//Load PRSP configure file
		if(!is_file(dirname ( __FILE__ ) . '/Config.php') ){
			die('PRSP Configure File not found');
		}else{
			include_once dirname ( __FILE__ ) . '/Config.php';
		}
		
		//Set PHP error report style
		if ($GLOBALS ['PRSP']['ErrorReport']) {
			error_reporting ( E_ALL );
		} else {
			error_reporting ( 0 );
		}
		
		//Constant setup
		define ( 'ROOT_PATH'     , $_SERVER ['DOCUMENT_ROOT'] );
		define ( 'PLUGINS_PATH'  , ROOT_PATH . $GLOBALS['PRSP']['Folders']['Plugins'] );
		
		define ( 'PRSP_HOME'     , dirname ( __FILE__ ) );
		define ( 'PLUGINS_FOLDER', $GLOBALS['PRSP']['Folders']['Plugins'] );
		define ( 'CONFIG_FOLDER' , $GLOBALS['PRSP']['Folders']['Configuration'] );
		define ( 'LIBRARY_FOLDER', $GLOBALS['PRSP']['Folders']['Library'] );

		//Start session
		@Session_start();
		
		//Load PRSP framework core
		include_once PRSP_HOME . '/Register.class.php';
		include_once PRSP_HOME . '/Runtime.class.php';
		include_once PRSP_HOME . '/ClassLoader.class.php';
		include_once PRSP_HOME . '/PrspLibrary.class.php';
		include_once PRSP_HOME . '/PrspActivator.interface.php';
		
		Register::setDefaultController( $GLOBALS['PRSP']['DefaultController'] );

		//installer
		if ( !is_dir ( PLUGINS_PATH )) {
			die('Plugin path not fount : '.PLUGINS_PATH);
		}else{
			//sacn plugin dir
			$pluginsDir = @opendir ( PLUGINS_PATH );
			while ( ($plugin = readdir ( $pluginsDir )) != false ) {
				if ( substr($plugin,0,1)!== '.' && is_dir ( PLUGINS_PATH . '/' . $plugin )) {
					Register::$plugins [$plugin]['Status'] = 'UNINSTALLED';
					Register::$plugins [$plugin]['Enabled'] = false;
					Register::$plugins [$plugin]['Depends'] = Array();
					Register::$plugins [$plugin]['Version'] = '0.0';
					Register::$plugins [$plugin]['AutoStart'] = false;
					//install plugin
					if ( is_file ( PLUGINS_PATH . '/' . $plugin . '/plugin.php' )) {
						Runtime::install($plugin);
					}		
				}
			}
			closedir ( $pluginsDir );
			//Process auto active plugin
			foreach ( array_keys(Register::getPlugins()) as $plugin ) {
				if( Register::$plugins[$plugin]['Status']==='RESOLVED' && Register::$plugins[$plugin]['AutoStart'] ){
					Runtime::start($plugin);
				}
			}
		}

	}
	
	/**
	 * PRSP Dispatcher<br>
	 * Find the executable when the controller then return true otherwise return false.
	 *
	 * @return boolean
	 */
	public function dispatcher() {
		if( !isset($_SERVER['REQUEST_URI']) ){
			die('$_SERVER[\'REQUEST_URI\'] is null');
		}else{
			
			if($_SERVER['REQUEST_URI']==='/' || strpos($_SERVER['REQUEST_URI'],'?')==1 ){
				//default entry point
				$controller = Register::getDefaultController();
			}else if( substr_count($_SERVER['REQUEST_URI'],'/')==1 ){
				$controller = substr($_SERVER['REQUEST_URI'], 1);
			}else{
				$controller = substr($_SERVER['REQUEST_URI'], 1, strpos(substr( $_SERVER['REQUEST_URI'], 1), '/'));
			}
			
			//fixed url
			if( substr($controller, strlen($controller)-1)==='/' ){
				$controller = substr($controller, 0, strlen($controller)-2);
			}
			if( strpos($controller,'?') > 0 ){
				$controller = substr($controller,0,strpos($controller,'?'));
			}
			Register::setControllerName($controller);
			//$controller.substr()
			$findController = false;
			$plugins = Register::getPlugins();
			foreach ( array_keys($plugins) as $plugin ) {
				if( $plugins[$plugin]['Status']!=='INSTALLED' && $plugins[$plugin]['Status']!=='UNINSTALLED' ){
					$controllerFile = PLUGINS_PATH . '/' . $plugin . '/controller/' .$controller.'.php';
					if( is_file($controllerFile) ){
						Register::addControllers( $controllerFile, $plugin);
						Runtime::start($plugin);
						Runtime::invoke('/controller/' .$controller.'.php', $plugin);
						$findController = true;
						break;
					}
				}
			}
			if( $findController ){
				return true;
			}
		}
		return false;
	}
	
	/**
	 * PRSP Closer<br>
	 * Stop all of plugins.
	 *
	 */
	public function closer() {
		while(count(Register::$startPluginList)>0){
			Runtime::stop( array_pop(Register::$startPluginList) );
		}
	}
	
}

?>